package com.gtq.uhome.service.serviceImpl;

import com.gtq.uhome.mapper.SiteMapper;
import com.gtq.uhome.mapper.SiteSupRelationMapper;
import com.gtq.uhome.mapper.SuppliesManagementMapper;
import com.gtq.uhome.mapper.SuppliesMapper;
import com.gtq.uhome.service.SuppliesManagementService;
import com.gtq.uhome.vo.SuppliesVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SuppliesManagementServiceImpl implements SuppliesManagementService {
    @Resource
    private SuppliesManagementMapper suppliesManagementMapper;
    @Resource
    private SiteSupRelationMapper siteSupRelationMapper;
    @Resource
    private SuppliesMapper suppliesMapper;

    /**
     * 添加物料
     *
     * @param suppliesVO 物料VO对象
     * @return true 添加成功 false 添加失败
     */
    @Override
    public boolean addSupplies(SuppliesVO suppliesVO) {
        //计算总成本
        suppliesVO.setTotalCost(suppliesVO.getUnitCost()*suppliesVO.getSupCount());
        //添加物料信息
        Integer integer = suppliesManagementMapper.addSupplies(suppliesVO);
        if (integer == 1) {
            //查询刚添加物料信息
            Integer supId = suppliesMapper.selectIdByObject(suppliesVO);
            String siteName = suppliesVO.getSiteName();
            //查询站点的id
            int siteId = suppliesManagementMapper.siteNameBySiteId(siteName);
            //添加物料与站点关联表
            Integer sup = siteSupRelationMapper.insertSupSiteRelation(siteId, supId);
            if (sup == 1)
                return true;
        }
        return false;
    }

    /**
     * 修改物料信息
     *
     * @param suppliesVO 物料VO对象
     * @return true 添加成功 false 添加失败
     */
    @Override
    public boolean updateSupplies(SuppliesVO suppliesVO) {
        if (suppliesVO != null) {
            String siteName = suppliesVO.getSiteName();
            Integer setSite = -1;
            if (siteName != null) {
                //查询站点名称对应的ID
                Integer siteId = suppliesManagementMapper.siteNameBySiteId(siteName);
                //修改物料对应站点的ID
                setSite = suppliesManagementMapper.updataSuppliesSite(suppliesVO.getSupId(),suppliesVO.getSiteId(),siteId);
            }
            //修改物料的信息
            Integer integer = suppliesManagementMapper.updataSupplies(suppliesVO);
            //返回影响的行数
            if (integer == 1 || setSite == 1)
                return true;
        }
        return false;


    }

    /**
     * 批量删除物料信息
     *
     * @param suppLiesId 要删除物料ID
     * @return true 添加成功 false 添加失败
     */
    @Override
    public Boolean deleteSupplies(List<Integer> suppLiesId,List<Integer> deleteSiteId) {
        //删除关联表
        Integer integer=-1;
        for (int i = 0; i < suppLiesId.size(); i++) {
             integer = suppliesManagementMapper.deleteSuppliesRelation(suppLiesId.get(i), deleteSiteId.get(i));
        }
        //删除的物料id
        //Integer integer = suppliesManagementMapper.deleteSuppliesId(suppLiesId);
        return integer > 0;
    }


    /**
     * 分页查询 全部物料
     *
     * @return 返回一个物料Vo对象
     */
    @Override
    public List<SuppliesVO> pageSelectSupplies() {
        List<SuppliesVO> suppliesVOS = suppliesManagementMapper.pageSelectSupplies();
        return suppliesVOS;
    }

    /**
     * 模糊查询站点的物料信息
     *
     * @param suppliesName 物料名称
     * @param siteName     站点名称
     * //@param page         当前页数
     * //@param limit        每页条数
     * @return 返回Vo对象
     */
    @Override
    public List<SuppliesVO> blurrySelectSupplies(String suppliesName, String siteName) {
        return suppliesManagementMapper.blurrySelectSupplies(suppliesName, siteName);
    }

    /**
     * 查询物料总条数
     *
     * @return 返回物料总条数
     */
    @Override
    public Integer selectCount() {
        return suppliesManagementMapper.selectCount();
    }

    /**
     * 查询站点物料总条数
     *
     * @param siteName 查询站点总条数
     * @return 返回站点总条数
     */
    @Override
    public Integer selectSiteCount(String siteName) {
        Integer siteCount = suppliesManagementMapper.selectSiteCount(siteName);
        return siteCount;
    }
}
